package de.kuaile.wx.httpclient;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.httpclient.NameValuePair;

public abstract class AbstractRequest {
	
	private List<NameValuePair> pairs;
	
	public AbstractRequest(){
		this.pairs = new ArrayList<NameValuePair>();
	}
	
	public void add(NameValuePair valuePair){
		this.pairs.add(valuePair);
	}
	
	public void add(String name, String value){
		this.add(new NameValuePair(name, value));
	}
	
	public NameValuePair[] toArray(){
		return this.pairs.toArray(new NameValuePair[this.pairs.size()]);
	}
	
	public void sort(){
		Collections.sort(this.pairs, new  Comparator<NameValuePair>() {
			public int compare(NameValuePair o1, NameValuePair o2) {
				int i = o1.getName().compareTo(o2.getName());
				if (i == 0) {
					o1.getValue().compareTo(o2.getValue());
				}
				return i;
			}
		});
	}
}
